<?php
class IndexAjaxController extends AjaxController {
    /**
     * 获得直播类型的列表
     *
     */
    public function getLiveList() {
        $data = M('LiveType')->getAllData();
        $this->_data['data'] = $data;
        $this->echoJson();
    }
    /**
     * 获得直播列表 根据当前人数进行排行
     * @param type_id int 类型id 根据类型id进行筛选
     * @param type_flag int 分类标示 根据类型id进行分类筛选
     */
    public function getLiveRoomList() {
        $type_id = intval(getParam('type_id', 0));
        $type_flag = intval(getParam('type_flag' , 0));
        $all_flag = intval(getParam('all_flag', 0));
        $page = intval(getParam('page', 1));
        $num = intval(getParam('num', 10));
        $where = array();
        if($type_id != 0) {
            $where['type_id'] = $type_id;
        }
        $where['status'] = array('in', array(0,1));
        //$data = D('Room')->getDataByField($where);

        if($all_flag == 0) {
            $data = D('Room')->getPageDataByField($where, $page, $num);

        }else {
            $data = D('Room')->getDataByField($where, "user_nr desc");

        }
        $type_ids = setArrayByFieldValue($data, 'type_id');
        $type_infos = D('Type')->getDataByIds($type_ids);
        $type_infos = setArrayByField($type_infos);
        $user_ids = setArrayByFieldValue($data, 'user_id');
        $user_infos = D('User')->getDataByIds($user_ids);
        $user_infos = setArrayByField($user_infos);
        foreach ($data as $key => &$value) {
            $value['type_name'] = $type_infos[$value['type_id']]['name'];
            $value['user_info'] = $user_infos[$value['user_id']];
        }
        if($type_flag == 1) {
            $type_ids = setArrayByFieldValue($data, 'type_id');
            $type_infos = D('Type')->getDataByIds($type_ids);
            $types_infos = setArrayByField($type_infos);
            $data = arrayGroupByKey($data, 'type_id');
            foreach ($data as $key => &$value) {
                $datas['list'] = $value;
                $datas['info'] = $types_infos[$key];
                $dataes[] = $datas;
            }
        }else{
            $dataes = $data;
        }


        $this->_data['data'] = $dataes;
        $this->echoJson();
    }
}